package tq.com.tqstudent.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.MalistBean;
import tq.com.tqstudent.ui.activity.classes.ChatActivity;
import tq.com.tqstudent.utils.VarietyUtil;
import tq.com.tqstudent.widgets.PreviewImgActivity;

/**
 * @Author rames
 * @date 2019/4/3 09:55
 */
public class ShareListAdapter extends RecyclerView.Adapter {
    private List<MalistBean> mDatas = new ArrayList<>();
    private Context mContext;
    private View mHeaderView;
    private View mFooterView;
    private View mEmptyView;
    private ShareListAdapter.OnItemClickListener listener;

    private int ITEM_TYPE_NORMAL = 0;
    private int ITEM_TYPE_HEADER = 1;
    private int ITEM_TYPE_FOOTER = 2;
    private int ITEM_TYPE_EMPTY = 3;

    public ShareListAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<MalistBean> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    public void addData(List<MalistBean> datas){
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public List<MalistBean> getDatas(){
        return mDatas;
    }

    public void setOnItemClickListener(ShareListAdapter.OnItemClickListener listener){
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    // 创建视图
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) {
            return new ShareListAdapter.ViewHolder(mHeaderView);
        } else if (viewType == ITEM_TYPE_EMPTY) {
            return new ShareListAdapter.ViewHolder(mEmptyView);
        } else if (viewType == ITEM_TYPE_FOOTER) {
            return new ShareListAdapter.ViewHolder(mFooterView);
        } else {
            View v = LayoutInflater.from(mContext).inflate( R.layout.item_share_list, parent,false);
            return new ShareListAdapter.ViewHolder(v);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (null != mHeaderView && position == 0) {
            return ITEM_TYPE_HEADER;
        }
        if (null != mFooterView && position == getItemCount() - 1) {
            return ITEM_TYPE_FOOTER;
        }
        if (null != mEmptyView && mDatas.size() == 0) {
            return ITEM_TYPE_EMPTY;
        }
        return ITEM_TYPE_NORMAL;

    }

    // 为Item绑定数据
    @Override
    public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == ITEM_TYPE_HEADER || type == ITEM_TYPE_FOOTER || type == ITEM_TYPE_EMPTY) {
            return;
        }

        int realPos = getRealItemPosition(position);
        MalistBean bean = mDatas.get(realPos);
        ShareListAdapter.ViewHolder vh = ((ShareListAdapter.ViewHolder) holder);
        vh.titleTV.setText(bean.title);
        vh.nameTV.setText(bean.author.name);
        vh.lookCountTV.setText(String.valueOf(bean.view_cnt));
        vh.commentCountTV.setText(String.valueOf(bean.comment_cnt));

        if(!TextUtils.isEmpty(bean.author.pic)) {
            Glide.with(mContext).load(VarietyUtil.getHeadImgUri(bean.author.pic)).into(vh.headIV);
        }else{
            vh.headIV.setImageResource(R.mipmap.icon_head_default);
        }
        vh.indexTV.setText("第"+String.valueOf(position)+"份");

        vh.commentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatActivity.show((Activity)mContext, true, "分享", bean.id, bean.id);
            }
        });

        GridView gridView = vh.gridView;
        ShareItemAdapter adapter = new ShareItemAdapter(mContext);
        gridView.setAdapter(adapter);
        adapter.setData(getList(bean.content));
        adapter.notifyDataSetChanged();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                PreviewImgActivity.show((Activity) mContext, getList(bean.content), i, bean.id);
            }
        });
    }

    private ArrayList<String> getList(String content){
        String[] array = content.split(",");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            list.add(App.getFileUrl(array[i]));
        }

        return list;
    }

    private int getRealItemPosition(int position) {
        if (null != mHeaderView) {
            return position - 1;
        }
        return position;
    }

    @Override
    public int getItemCount() {
        int itemCount = mDatas.size();
        if (null != mEmptyView && itemCount == 0) {
            itemCount++;
        }
        if (null != mHeaderView) {
            itemCount++;
        }
        if (null != mFooterView) {
            itemCount++;
        }
        return itemCount;
    }

    public void addHeaderView(View view) {
        mHeaderView = view;
        notifyItemInserted(0);
    }

    public void addFooterView(View view) {
        mFooterView = view;
        notifyItemInserted(getItemCount() - 1);
    }

    public void setEmptyView(View view) {
        mEmptyView = view;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleTV, nameTV, indexTV, lookCountTV, commentCountTV;
        GridView gridView;
        ImageView headIV;
        LinearLayout commentLayout;

        ViewHolder(View v) {
            super(v);
            headIV = v.findViewById(R.id.head_iv);
            titleTV = v.findViewById(R.id.title_tv);
            nameTV = v.findViewById(R.id.name_tv);
            indexTV = v.findViewById(R.id.index_tv);
            gridView = v.findViewById(R.id.gridview);
            lookCountTV = v.findViewById(R.id.look_count_tv);
            commentLayout = v.findViewById(R.id.comment_layout);
            commentCountTV = v.findViewById(R.id.comment_count_tv);

            v.setOnClickListener(view -> {
                if(null != listener){
                    listener.onItemClick(v, getAdapterPosition());
                }
            });
        }
    }
}
